//
//  UIViewExt.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/5/25.
//

import Foundation


// MARK: - Frame
public extension UIView {
    
    var width: CGFloat {
        get {
            return frame.size.width
        }
        set {
            frame.size.width = newValue
        }
    }
    
    var height: CGFloat {
        get {
            return frame.size.height
        }
        set {
            frame.size.height = newValue
        }
    }
    
    var size: CGSize {
        get {
            return frame.size
        }
        set {
            width = newValue.width
            height = newValue.height
        }
    }
    
    var x: CGFloat {
        get {
            return frame.origin.x
        }
        set {
            frame.origin.x = newValue
        }
    }
    
    var y: CGFloat {
        get {
            return frame.origin.y
        }
        set {
            frame.origin.y = newValue
        }
    }
    
}

// MARK: - Layer
public extension UIView {
    
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.masksToBounds = true
            layer.cornerRadius = abs(CGFloat(Int(newValue * 100)) / 100)
        }
    }
    
    var borderColor: UIColor? {
        get {
            guard let color = layer.borderColor else { return nil }
            return UIColor(cgColor: color)
        }
        set {
            guard let color = newValue else {
                layer.borderColor = nil
                return
            }
            guard String(describing: type(of: color)) != "__NSCFType" else {return}
            layer.borderColor = color.cgColor
        }
    }
    
    var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }
    
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }
    
}

//MARK: - Tap
typealias VoidClosure = ()->Void
extension UIView{
    
    private struct AssociateKeys{
        static var key: Character = "\0"
    }
    
    var clickClosure: VoidClosure?{
        get{
            objc_getAssociatedObject(self, &AssociateKeys.key) as? VoidClosure
        }
        set{
            if let _ = objc_getAssociatedObject(self, &AssociateKeys.key) as? VoidClosure{
                
            }else{
                addTapGesture()
                objc_setAssociatedObject(self, &AssociateKeys.key, newValue, .OBJC_ASSOCIATION_COPY)
            }
        }
    }
    
    func addTapGesture(){
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(clickAction(gesture:)))
        addGestureRecognizer(singleTap)
        isUserInteractionEnabled = true
    }
    
    @objc func clickAction(gesture: UITapGestureRecognizer){
        clickClosure?()
    }
    
}
